package me.firstapp.bts.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.bts.service.OrderJobService;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.module.weixin.order.KaMiOrder;
import me.firstapp.module.weixin.order.ProductOrder;
import me.firstapp.module.weixin.order.SoftOrder;
import me.firstapp.module.weixin.order.SpreadOrder;
import me.firstapp.module.weixin.system.ThisSystem;
import me.firstapp.module.weixin.transfer.TransferMemberRecord;
import me.firstapp.service.weixin.member.SpreadOrderService;
import me.firstapp.service.weixin.product.ProductOrderService;
import me.firstapp.service.weixin.soft.SoftOrderService;
import me.firstapp.service.weixin.system.ThisSystemService;
import me.firstapp.service.weixin.transfer.TransferMemberRecordService;

@Service
public class OrderJobServiceImpl implements OrderJobService {

	private static Logger logger = LoggerFactory.getLogger(OrderJobServiceImpl.class);

	@Autowired
	private SoftOrderService softOrderService;

	@Autowired
	private ThisSystemService thisSystemService;

	@Autowired
	private SpreadOrderService spreadOrderService;

	@Autowired
	private ProductOrderService productOrderService;
	
	@Autowired
	private TransferMemberRecordService transferMemberRecordService;

	@Override
	public void cancelExpireOrders() {
		try {
			ThisSystem thisSystem = thisSystemService.findThisSystem();
			List<SoftOrder> softOrders = softOrderService.findOrders(SoftOrder.STATUS_NOT_PAY);
			logger.info("未支付软件订单数为：" + softOrders.size());
			for (SoftOrder softOrder : softOrders) {
				long minutes = DateUtils.getDistanceTimesForMinute(new Date(), softOrder.getCreateTime());
				if (minutes >= thisSystem.getOrderOutTimes()) {
					softOrder.setStatus(KaMiOrder.STATUS_CANCEL);
					softOrder.setCancelTime(new Date());
					softOrderService.executeUpdate(softOrder);
					logger.info(
							"软件订单【" + softOrder.getOrderNo() + "】未支付时间超过" + thisSystem.getOrderOutTimes() + "分钟，已被取消。");
				}
			}

			List<ProductOrder> productOrders = productOrderService.findOrders(ProductOrder.STATUS_NOT_PAY);
			logger.info("未支付商品订单数为：" + productOrders.size());
			for (ProductOrder productOrder : productOrders) {
				long minutes = DateUtils.getDistanceTimesForMinute(new Date(), productOrder.getCreateTime());
				if (minutes >= thisSystem.getOrderOutTimes()) {
					productOrderService.executeCancelOrder(productOrder);
					logger.info("商品订单【" + productOrder.getOrderNo() + "】未支付时间超过" + thisSystem.getOrderOutTimes()
							+ "分钟，已被取消。");
				}
			}
			
			List<TransferMemberRecord> transferMemberRecords = transferMemberRecordService
					.findByStatus(TransferMemberRecord.STATUS_NOT_PAY);
			logger.info("未支付转让订单数为：" + transferMemberRecords.size());
			for (TransferMemberRecord transferMemberRecord : transferMemberRecords) {
				long minutes = DateUtils.getDistanceTimesForMinute(new Date(), transferMemberRecord.getCreateTime());
				if (minutes >= 10) {
					TransferMemberRecord newTransferMemberRecord = transferMemberRecordService
							.executeCancel(transferMemberRecord);
					if (TransferMemberRecord.STATUS_CANCEL == newTransferMemberRecord.getStatus()) {
						logger.info("转让订单【" + transferMemberRecord.getRecordNo() + "】未支付时间超过10分钟，已被取消。");
					}
				}
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("取消订单时遇到错误,错误码：" + e.getCode() + "，错误信息：" + e.getMessage(), e);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("取消超时订单时遇到错误", e);
		}

	}

	@Override
	public void settleOrders() {
		try {
			List<SoftOrder> softOrders = softOrderService.findOrders(SoftOrder.STATUS_COMPLETE,
					SoftOrder.SETTLE_FLAG_NOT);
			logger.info("待结算的软件订单数为：" + softOrders.size());
			for (SoftOrder softOrder : softOrders) {
				logger.info("软件订单【" + softOrder.getOrderNo() + "】执行结算 start");
				softOrderService.executeSettle(softOrder);
				logger.info("软件订单【" + softOrder.getOrderNo() + "】执行结算 end");
			}
			List<SpreadOrder> spreadOrders = spreadOrderService.findBySettleFlag(SpreadOrder.SETTLE_FLAG_NOT);
			logger.info("待结算的推广单数为：" + spreadOrders.size());
			for (SpreadOrder spreadOrder : spreadOrders) {
				logger.info("推广单【" + spreadOrder.getId() + "】执行结算 start");
				spreadOrderService.executeSettle(spreadOrder);
				logger.info("推广单【" + spreadOrder.getId() + "】执行结算 end");
			}
			List<ProductOrder> productOrders = productOrderService.findOrders(ProductOrder.STATUS_COMPLETE,
					ProductOrder.SETTLE_FLAG_NOT);
			logger.info("待结算的商品订单数为：" + productOrders.size());
			for (ProductOrder productOrder : productOrders) {
				logger.info("商品订单【" + productOrder.getOrderNo() + "】执行结算 start");
				productOrderService.executeSettle(productOrder);
				logger.info("商品订单【" + productOrder.getOrderNo() + "】执行结算 end");
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("结算订单时遇到错误,错误码：" + e.getCode() + "，错误信息：" + e.getMessage(), e);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("结算订单时遇到错误", e);
		}

	}

}
